package com.ssfk.service.mybatis.mapper;

import com.ssfk.service.mybatis.auto.Statement;
import com.ssfk.service.mybatis.auto.StatementHistory;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;
import java.util.Map;

public interface StatementMapper {

    class SQLProvider {
        public String addHistory(final StatementHistory data) {
            String sql = new SQL() {
                {
                    INSERT_INTO("t_statement_history");
                    if (null != data.getTable_name()) {
                        VALUES("table_name", "#{table_name}");
                    }

                    if (null != data.getAlias_name()) {
                        VALUES("alias_name", "#{alias_name}");
                    }
                    if (null != data.getSql()) {
                        VALUES("`sql`", "#{sql}");
                    }

                    if (null != data.getStatement()) {
                        VALUES("statement", "#{statement}");
                    }

                    if (null != data.getEditor_name()) {
                        VALUES("editor_name", "#{editor_name}");
                    }

                    if (null != data.getEditor_uid()) {
                        VALUES("editor_uid", "#{editor_uid}");
                    }

                    VALUES("create_time", "#{create_time}");
                }
            }.toString();
            return sql;
        }
    }


    @Select("Select * from t_statement where name=#{name}")
    Statement getStatement(@Param("name") String name);

    @Select("${statement}")
    List<Map<String, Object>> selectMuti(@Param("statement") String statement);

    @Select("${statement}")
    Map<String, Object> select(@Param("statement") String statement);

    @Select("${statement}")
    @Options(statementType = StatementType.CALLABLE)
    List<Map<String, Object>> callable(@Param("statement") String callable);

    @Delete("${statement}")
    Integer delete(@Param("statement") String statement);

    @Select("${statement}")
    Integer count(@Param("statement") String statement);

    @Update("${statement}")
    Integer update(@Param("statement") String statement);

    @Insert("${statement}")
    Integer insert(@Param("statement") String statement);

    @InsertProvider(type = SQLProvider.class, method = "addHistory")
    Integer addHistory(StatementHistory history);
}